package com.wtao.testcamera;

import android.graphics.SurfaceTexture;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.senseauto.subgroup.autocameralib.callback.CameraImageCallback;
import com.senseauto.subgroup.autocameralib.cameraV2.CameraV2Proxy;
import com.senseauto.subgroup.autocameralib.model.CameraOptions;
import com.senseauto.subgroup.autocommonlib.utilcode.util.ImageUtils;
import com.wtao.testcamera.strategy.RecordStrategy;
import com.wtao.testcamera.utils.FileUtils;
import com.wtao.testcamera.utils.RecordManager;


public class CameraActivity extends AppCompatActivity implements CameraImageCallback, View.OnClickListener {
    String TAG = "CameraActivity";
    TextureView textureView;
    CameraV2Proxy cameraV2Proxy;
    Button btnRecord;
    int cameraId = ConstantsGAC.CAMERA_ID_DMS;
    boolean flagRecoding = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        textureView = findViewById(R.id.texture);
        findViewById(R.id.btnBack).setOnClickListener(v->{
            finish();
        });
        //录制初始化
        RecordStrategy recordStrategy = new RecordStrategy()
                .setContinuousRecord(true)
                .setRecordMaxTime(30 * 1000)
                .setVideoDir(FileUtils.outputRecordVideoPath(this))
                .setVideoHeight(ConstantsGAC.SDK_INPUT_IMG_H)
                .setVideoWidth(ConstantsGAC.SDK_INPUT_IMG_W);
        RecordManager.getInstance().init(this.getApplicationContext(), recordStrategy);
        btnRecord = findViewById(R.id.record);
        btnRecord.setOnClickListener(this);
        findViewById(R.id.switchCamera).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cameraV2Proxy != null) {
                    cameraV2Proxy.releaseCamera();
                    cameraV2Proxy = null;
                }
                if (cameraId == 45) {
                    cameraId = 49;
                } else if (cameraId == 49) {
                    cameraId = 45;
                }

                CameraOptions cameraOptions = new CameraOptions();
                cameraOptions.setCameraId(cameraId);
                cameraOptions.setImgWidth(1600);
                cameraOptions.setImgHeight(1300);
                cameraOptions.setImageFormat(ImageUtils.ImageFormat.YUV420SP);
                if (cameraV2Proxy == null) {
                    cameraV2Proxy = new CameraV2Proxy(getApplicationContext(), cameraOptions);
                }
                cameraV2Proxy.setCameraImageCallback(CameraActivity.this);
                cameraV2Proxy.initOption();
                textureView.setScaleX(-1f);
                cameraV2Proxy.setPreview(new Surface(textureView.getSurfaceTexture()));
                cameraV2Proxy.openCamera();
            }
        });
        initOpenCamera();
    }

    public void initOpenCamera() {
        CameraOptions cameraOptions = new CameraOptions();
        cameraOptions.setCameraId(cameraId);
        cameraOptions.setImgWidth(1600);
        cameraOptions.setImgHeight(1300);
        cameraOptions.setImageFormat(ImageUtils.ImageFormat.YUV420SP);
        if (cameraV2Proxy == null) {
            cameraV2Proxy = new CameraV2Proxy(this, cameraOptions);
        }
        cameraV2Proxy.setCameraImageCallback(this);
        cameraV2Proxy.initOption();
        textureView.setScaleX(-1f);
        textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {
                cameraV2Proxy.setPreview(new Surface(surface));
                cameraV2Proxy.openCamera();
            }

            @Override
            public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) {

            }

            @Override
            public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {
                return false;
            }

            @Override
            public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {

            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        cameraV2Proxy.releaseCamera();
        RecordManager.getInstance().unInit();
    }

    //获取到了数据,开始编码---start
    @Override
    public void onReadImageData(@Nullable byte[] imageData, int i, int i1) {
        RecordManager.getInstance().encodeFrame(imageData);
        if(flagRecoding){
            Log.e(TAG, "onReadImageData-->");
        }
    }

    @Override
    public void onException(@NonNull Exception e) {

    }
    //获取到了数据,开始编码---end

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.record:
                if (!flagRecoding) {
                    flagRecoding = true;
                    btnRecord.setText("录制中..");
                    RecordManager.getInstance().start();
                } else {
                    flagRecoding = false;
                    btnRecord.setText("录制视频");
                    new Handler().postDelayed(() -> {
                        RecordManager.getInstance().stop();
                    }, 300);
                }
                break;
        }
    }

}